import 'package:flutter/material.dart';
import 'package:flutter_trip/dao/travel_tab_dao.dart';
import 'package:flutter_trip/model/travel_tab_model.dart';
import 'package:flutter_trip/pages/travel_tab_page.dart';

class TravelPage extends StatefulWidget {
  const TravelPage({Key? key}) : super(key: key);

  @override
  _TravelPageState createState() => _TravelPageState();
}

class _TravelPageState extends State<TravelPage> with TickerProviderStateMixin {
  TravelTabModel? travelTabModel;
  late TabController _controller;
  List<TravelTab> tabs = [];

  @override
  void initState() {
    _controller = TabController(length: 0, vsync: this);
    TravelTabDao.fetch().then((model) {
      _controller = TabController(length: model.tabs.length, vsync: this);
      setState(() {
        travelTabModel = model;
        tabs = model.tabs;
      });
    }).catchError((e) {
      print(e);
    });
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Container(
            color: Colors.white,
            padding: const EdgeInsets.only(top: 30),
            child: TabBar(
              controller: _controller,
              isScrollable: true,
              labelColor: Colors.black,
              labelPadding: EdgeInsets.fromLTRB(20, 0, 10, 5),
              indicator: const UnderlineTabIndicator(
                  borderSide: BorderSide(width: 3, color: Color(0xff2fcfbb)),
                  insets: EdgeInsets.only(bottom: 10)),
              tabs: tabs
                  .map((tab) => Tab(
                        text: tab.labelName,
                      ))
                  .toList(),
            ),
          ),
          Flexible(
              child: TabBarView(
            controller: _controller,
            children: tabs.map((tab) {
              return TravelTabPage(
                travelUrl: travelTabModel!.url,
                params: travelTabModel!.params  ,
                groupChannelCode: tab.groupChannelCode,
              );
            }).toList(),
          ))
        ],
      ),
    );
  }
}
